package com.llh.controller.http;

import com.llh.bean.Express;
import com.llh.bean.Income;
import com.llh.bean.MiddleData;
import com.llh.controller.http.common.CommonExcuteTrade;
import com.llh.mapper.ExpressMapper;
import com.llh.mapper.IncomeMapper;
import com.llh.util.SqlSessionUtil;
import lombok.Data;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;
@Data
public class IncomeTrade extends CommonExcuteTrade {
    private MiddleData middleData;
    static Logger logger = LogManager.getLogger(FinaIndicatorTrade.class.getName());

    @Override
    public void parseParam(String[][] items, String[] fields) throws Exception {
        //获取sqlSession
        SqlSession sqlSesion = null;

        //该接口只能一次调一只股票的,这里我们根据查询条件只会返回一条数据
        List<Income> list = (List<Income>) copyProperty(items, fields);
            /*if(list.size()!=1){
                throw new Exception("数据有误");
            }*/
        if (list == null || list.size() == 0) {
            logger.error("没有获取到任何数据");
            return;
        }
        Income income = list.get(0);
        income.setOpen_day(middleData.getOpen_day());
        try {
            sqlSesion = SqlSessionUtil.getSqlSesion();
            IncomeMapper mapper = sqlSesion.getMapper(IncomeMapper.class);
            logger.info("插入数据");
            mapper.insertSelective(income);
            logger.info("插入操作成功");

        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("将数据插入到express失败");
        } finally {
            sqlSesion.close();
        }
    }

    @Override
    public List copyProperty(String[][] items, String[] fields) throws Exception {
        List<Income> incomes = new LinkedList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        if (items == null || items.length == 0) {
            //没有数据则返回空list
            return incomes;
        }
        for (int j = 0; j < items.length; j++) {
            //参数值数组
            String[] item = items[j];
            if (item == null || item.length == 0) {
                //如果有数据组为null,为空数组,跳下一次循环
                continue;
            }
            Income income = new Income();
            Field[] expressFields = income.getClass().getDeclaredFields();

            //有效数据数据格式校验
            if (item.length != fields.length) {
                throw new Exception("响应的数据有误");
            }
            for (int i = 0; i < fields.length; i++) {
                //参数名
                String field = fields[i];
                //参数值
                String value = item[i];
                for (Field declaredField : expressFields) {
                    declaredField.setAccessible(true);
                    String fieldName = declaredField.getName();
                    if (field.equals(fieldName)) {
                        //如果返回来的值为null,字符串"null",空字符串"",将对应参数设置为null
                        if (value == null || "null".equals(value.toLowerCase()) || "".equals(value)) {
                            declaredField.set(income, null);
                        } else {
                            //若果是正常值,则转换为对应的类型
                            if (field.equals("end_date")) {
                                //将字符串转换成date.并赋值
                                declaredField.set(income, format.parse(value));
                            } else if (field.equals("revenue") || field.equals("compr_inc_attr_p")) {
                                //将字符串转换成double
                                declaredField.set(income, Double.valueOf(value));
                            } else {
                                //直接保留原字符串格式
                                declaredField.set(income, value);
                            }
                        }


                    }
                }
            }
            //添加到list集合
            incomes.add(income);

        }
        return incomes;
    }
}

